<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="co.kr.teamcloud.issue.service.*" %>
<%@ page import="com.google.appengine.api.users.*" %>
<%@ page import="com.google.appengine.api.datastore.*" %>       
<%
	int projectId = request.getParameter("projectId")==null?0:Integer.parseInt(request.getParameter("projectId"));	//프로젝트 ID	
	int issueId = request.getParameter("issueId")==null?0:Integer.parseInt(request.getParameter("issueId"));		//이슈 ID
	
	String issueSj = request.getParameter("issueSj");		//이슈 제목
	String issueCn = request.getParameter("issueCn");		//이슈 내용
    		
	UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    
    //로그인 사용자만 접근
    if( user != null ) {
	    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	    
	    //이슈 ID가 있는 경우에는 기존 데이터 수정
	    if(issueId>0) {
	    	Key issueKey = KeyFactory.createKey(KeyFactory.createKey("issue", projectId), "issue", issueId);
	    	Entity issue = datastore.get(issueKey);	
	    	
	    	User frstRegisterUser = (User) issue.getProperty("frstRegisterUser");
	    	
	    	//작성자가 동일한 경우에만 데이터 저장
	    	if(frstRegisterUser.equals(user)) {
		    	issue.setProperty("issueSj", issueSj);
			    issue.setProperty("issueCn", issueCn);
			    issue.setProperty("lastUpdusrId", user.getUserId());
			    issue.setProperty("lastUpdtPnttm", new Date());
			    issue.setProperty("lastUpdusrUser", user);
			    
			    //데이터 저장
			    datastore.put(issue);
	    	}
	    	
	    } else {
	    
		    //저장할 데이터
		    Key issueKey = KeyFactory.createKey("issue", projectId);
		    Entity issue = new Entity("issue", issueKey);
		    issue.setProperty("issueSj", issueSj);
		    issue.setProperty("issueCn", issueCn);
		    issue.setProperty("frstRegisterId", user.getUserId());
		    issue.setProperty("frstRegistPnttm", new Date());
		    issue.setProperty("frstRegisterUser", user);
		    
		    //데이터 저장
		    datastore.put(issue);
	    }
    }
    
    response.sendRedirect("/issue/issueList.jsp");
%>
